/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.datacenter.api.dto.*;
import org.cshoo.tattoo.datacenter.app.LookupAppService;
import org.cshoo.tattoo.datacenter.domain.po.Lookup;
import org.cshoo.tattoo.datacenter.domain.po.LookupItem;
import org.cshoo.tattoo.embed.infra.entity.PageQueryEntity;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Api(tags = "Lookup接口")
@RestController
@RequestMapping(value = "/lookup")
@Slf4j
public class LookupController {

    @Resource
    private LookupAppService lookupAppService;

    /**
     * 判断编号是否存在
     *
     * @param code 编号
     * @return 判断结果
     */
    @ApiOperation("校验lookup编号")
    @RequestMapping(value = "/checkLookupCodeExists", method = RequestMethod.GET)
    public ValidResp checkLookupCodeExists(@RequestParam String code) {
        log.info("code:{}", code);
        ValidResp result = new ValidResp();
        boolean exists = lookupAppService.checkLookupCodeExists(code);
        result.setValid(!exists);
        return result;
    }

    /**
     * 添加lookup
     *
     * @param addLookupReq 添加lookup request VO
     */
    @ApiOperation("添加lookup")
    @PostMapping(value = "/addLookup")
    public Response<?> addLookup(@RequestBody AddLookupReq addLookupReq) {
        log.info("addLookupReq:{}", addLookupReq);
        lookupAppService.addLookup(addLookupReq);
        return Response.builder().build();
    }

    /**
     * 修改lookup
     *
     * @param updateLookupReq 修改lookup request
     */
    @ApiOperation("修改lookup")
    @PutMapping(value = "/updateLookup")
    public Response<?> updateLookup(@RequestBody @Valid UpdateLookupReq updateLookupReq) {
        log.info("updateLookupReq:{}", updateLookupReq);
        lookupAppService.updateLookup(updateLookupReq);
        return Response.builder().build();
    }

    /**
     * 删除lookup
     *
     * @param id lookup id
     */
    @ApiOperation("删除lookup")
    @DeleteMapping(value = "/deleteLookup/{id}")
    public Response<?> deleteLookup(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        lookupAppService.deleteLookup(id);
        return Response.builder().build();
    }

    /**
     * 查询lookup
     *
     * @return lookup 列表
     */
    @ApiOperation("查询lookup列表")
    @GetMapping(value = "/queryLookups")
    @Deprecated
    public List<Lookup> queryLookups() {
        return lookupAppService.queryLookups();
    }

    /**
     * 分页查询lookup
     *
     * @param request 请求VO
     * @return 分页查询lookup对象
     */
    @ApiOperation("查询lookup列表")
    @PostMapping(value = "/page/queryLookups")
    public Page<Lookup> queryLookups(@RequestBody @Valid PageQueryEntity<String> request) {
        return lookupAppService.queryLookups(request);
    }

    /**
     * 判断编号是否存在
     *
     * @param code     lookup项编号
     * @param lookupId lookupId
     * @return 判断结果
     */
    @ApiOperation("校验lookup项编号")
    @RequestMapping(value = "/checkLookupItemCodeExists", method = RequestMethod.GET)
    public ValidResp checkLookupItemCodeExists(@RequestParam String code, @RequestParam Long lookupId) {
        log.info("code:{}", code);
        ValidResp result = new ValidResp();
        boolean exists = lookupAppService.checkLookupItemCodeExists(code, lookupId);
        result.setValid(!exists);
        return result;
    }

    /**
     * 添加lookup 项
     *
     * @param addLookupItemReq 添加lookup项VO
     */
    @ApiOperation("添加lookup项")
    @PostMapping(value = "/addLookupItem")
    public Response<?> addLookupItem(@RequestBody AddLookupItemReq addLookupItemReq) throws Exception {
        log.info("addLookupItemReq:{}", addLookupItemReq);
        lookupAppService.addLookupItem(addLookupItemReq);
        return Response.builder().build();
    }

    /**
     * 修改lookup项
     *
     * @param updateLookupItemReq 修改lookup项VO
     */
    @ApiOperation("修改lookup项")
    @PutMapping(value = "/updateLookupItem")
    public Response<?> updateLookupItem(@RequestBody UpdateLookupItemReq updateLookupItemReq) {
        log.info("updateLookupItemReq:{}", updateLookupItemReq);
        lookupAppService.updateLookupItem(updateLookupItemReq);
        return Response.builder().build();
    }

    /**
     * 删除lookup项
     *
     * @param id lookup项id
     */
    @ApiOperation("删除lookup项")
    @DeleteMapping(value = "/deleteLookupItem/{id}")
    public Response<?> deleteLookupItem(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        lookupAppService.deleteLookupItem(id);
        return Response.builder().build();
    }

    /**
     * 查询lookup项
     *
     * @param lookupId lookupId
     * @return lookup项
     */
    @ApiOperation("查询lookup项")
    @GetMapping(value = "/queryLookupItems/{lookupId}")
    public List<LookupItem> queryLookupItems(@PathVariable("lookupId") Long lookupId) {
        log.info("lookupId:{}", lookupId);
        return lookupAppService.queryLookupItems(lookupId);
    }

    /**
     * 查询lookup item的列表
     *
     * @param request 分页查询请求
     * @return lookup item 列表
     */
    @ApiOperation("查询lookup item列表")
    @PostMapping(value = "/page/queryLookupItems")
    public Page<LookupItem> queryLookupItems(@RequestBody @Valid PageQueryEntity<Long> request) {
        log.info("request:{}", request);
        return lookupAppService.queryLookupItems(request);
    }

    /**
     * 查询单个lookup
     *
     * @param code 编号
     * @return lookup信息
     */
    @ApiOperation("查询单个的lookup")
    @GetMapping(value = "/querySingleLookup/{code}")
    public QuerySingleLookupResp querySingleLookup(@PathVariable("code") String code) throws Exception {
        log.info("code:{}", code);
        return lookupAppService.querySingleLookup(code);
    }
}